using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace _1942
{
    /// <summary>
    /// To decide what kind of projectile we are creating
    /// </summary>
    public enum ProjectileType
    { 
        Player,
        Kamakazie,
	    Miner,
	    Hunter
    }

    /// <summary>
    /// This class is the class for the projectiles from the player
    /// </summary>
    public class Projectile : Sprite
    {
        #region Variables

        // Velocity
        protected Vector2 velocity;

        //Whose is this projectile
        private ProjectileType _typeOfProjectile;

        //Window size
        private static int windowHeight = -1;
        private static int windowWidth = -1;

        //Colordata for perpixel collision detection
        private Color[] texturesData;

        #endregion

        #region Constructors

        public Projectile(Game game, ref Texture2D theTexture, Vector2 origin, Vector2 velocity, ProjectileType source)
            : base(game, ref theTexture)
        {
            //Sets the window size
            if (windowHeight == -1)
            {
                windowHeight = Game.Window.ClientBounds.Height;
                windowWidth = Game.Window.ClientBounds.Width;
            }

            Position = origin;
            this.velocity = velocity;
            this._typeOfProjectile = source;

            Frames = new List<Rectangle>();

           
            switch (source)
            {
                //Kamikaze purple bullets animation
                case ProjectileType.Kamakazie:
                    {
                        Rectangle frame = new Rectangle( START_OFFSET + 32*3 + 3  , START_OFFSET + 32 * 7 + 7 , 31, 31);

                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);

                        break;
                    }

                // Player yellow bullets animation
                case ProjectileType.Player:
                    {
                        Rectangle frame = new Rectangle(START_OFFSET + 32 * 2 + 1, START_OFFSET + 32 * 6 + 5, 32, 32);

                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);


                        frame.X = START_OFFSET + 32 ;

                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        Frames.Add(frame);
                        break;
                    }

                case ProjectileType.Miner:
                    {
                        Rectangle frame = new Rectangle();
                        frame.X = 103;
                        frame.Y = 204;
                        frame.Width = 32;
                        frame.Height = 28;
                        Frames.Add(frame);

                        break;
                    }
                case ProjectileType.Hunter:
                    {
                        Rectangle frame = new Rectangle();
                        frame.X = 269;
                        frame.Y = 104;
                        frame.Width = 30;
                        frame.Height = 28;
                        Frames.Add(frame);
                        break;

                    }
                default:
                    {
                        throw new ArgumentException("ProjectileType wrong");
                    }
            }

            int tmpColorValue = Frames[0].Width * Frames[0].Height;
            
            texturesData = new Color[tmpColorValue];
            
            theTexture.GetData<Color>(0, Frames[0], texturesData, 0, tmpColorValue);
        }

        #endregion

        #region Methods 
        /// <summary>
        /// Update the projectiles Position
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            // Check if the projectile still visible
            if (
                (Position.Y >= windowHeight) ||
                (Position.Y <= 0) ||
                (Position.X >= windowWidth) ||
                (Position.X <= 0) 
                )
            {
                kill();
            }
            else
            {
                // Move projectile
                Position += velocity;

                base.Update(gameTime);
            }
        }

        //Kills the projectile
        internal void kill()
        {
            Position = new Vector2(-50, Position.Y );
            velocity = Vector2.Zero;
            Visible = false;
            Enabled = false;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Velocity
        /// </summary>
        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public Color[] TextureData
        {
            get { return texturesData; }
        }
        #endregion
    }
}